<?php

namespace App\Http\Controllers\Admin;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/admin/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    public function logIn(){
        if (Auth::check()){
            return view('admin.home');
        }
        return view('admin.login');
    }
    public function postLoginIn(Request $request){
        $userName = $request['username'];
        $password = $request['password'];
        $validateParams = [
            'email' => $userName,
            'password' => $password
        ];
        if (Auth::attempt($validateParams,1)) {
            return $this->returnInfo(true, '登录成功。');
        }else{
            return $this->returnInfo(false, '账号或密码输入错误。');
        }
    }

    private function returnInfo($res, $msg, $url='',$debug=''){
        return response()->json(['res'=>$res, 'info'=>$msg, 'url'=>$url, 'debug'=>$debug]);
    }

    function getLogout(){
        Auth::logout();
        return Redirect::to('/admin/login');
    }
}
